#include "RenzoMustHaveGL.h"
#include "Renzoderer.h"
#include "RenzoRenderBuffer.h"
#include "RenzoRenderSystem.h"
#include "RenzoDisplayGL.h"
#include "RenzoTimer.h"

namespace Renzo
{
DisplayGL* DisplayGL::obj = 0;
DisplayGL::DisplayGL(void)
{
	obj = this;
}

DisplayGL::~DisplayGL(void)
{
}

void DisplayGL::onStaticDisplay() {
	obj->onDisplay();
}

void DisplayGL::onStaticIdle() {
	obj->onIdle();
}

void DisplayGL::onDisplay() {
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// ask render buffer to render -> chain of responsibility pattern
	RenderBuffer* rbuf = Renzoderer::instance()->getActiveRenderSystem()->getRenderBuffer();
	rbuf->render();

	glutSwapBuffers();
}

void DisplayGL::onIdle() {
	// update
	Renzoderer::instance()->update();

	// render
	glutPostRedisplay();
	
	// sleep
	Renzoderer::instance()->getTimer()->sleep(1);
}

void DisplayGL::show() {
	// TODO: read window settings from config file and store in Renzoderer (WindowManager->window[0])
	// register callbacks
	glutDisplayFunc(onStaticDisplay);
	glutIdleFunc(onStaticIdle);

	// start
	glutMainLoop();
}


}
